﻿using Microsoft.Build.Evaluation;
using NuGet;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OD.ProjectCustomizationTool.Helpers
{
    internal static class IPackageExtensions
    {
        /// <summary>
        /// Retrieve all dependencies of a package and itself at the end.
        /// </summary>
        /// <param name="package">The source package to consider.</param>
        /// <returns>Returns all dependencies and the package.</returns>
        public static IEnumerable<IPackage> GetAllPackages(this IPackage package)
        {
            if (package == null)
            {   // This code has been generated from the 'argnull' custom snippet.
                throw new ArgumentNullException("package");
            }

            foreach (var dependencySet in package.DependencySets)
            {
                foreach (var dependency in dependencySet.Dependencies)
                {
                    yield return NuGetHelper.Current.GetLatestVersion(dependency.Id);
                }
            }
            yield return package;
        }
    }
}
